package com.all.play.mypsm.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.mypsm.App.GoodsInfoActivity;
import com.all.play.mypsm.R;
import com.all.play.mypsm.home.bean.GoodsInfo;
import com.all.play.mypsm.home.bean.HomeResaultBeanData;
import com.all.play.mypsm.utils.Constants;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.transformer.RotateDownTransformer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/3/31.
 */

public class HomeFragmentAdapter extends RecyclerView.Adapter {

    //广告类型
    public static final int BANNER=0;
    //频道类型
    public static final int CHANNEL=1;
    //活动类型
    public static final int ACT=2;
    //秒杀类型
    public static final int SECKILL=3;
    //推荐类型
    public static final int RECOMMEND=4;
    //热卖类型
    public static final int HOT=5;
    private static final String GOODSINFO = "goodsinfo";

    //定义当前类型
    private int currentType=BANNER;

    private Context mContext;
    private HomeResaultBeanData.ResultBean resultBean;
    private final LayoutInflater inflater;

    public HomeFragmentAdapter(Context mContext, HomeResaultBeanData.ResultBean resultBean) {
        this.mContext=mContext;
        this.resultBean=resultBean;
        inflater = LayoutInflater.from(mContext);
    }

    //创建viewholder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==BANNER){
            return new BannerViewHolder(mContext,inflater.inflate(R.layout.banner_viewpage,
                    null));
        }else if(viewType==CHANNEL){
            return new ChannelViewHolder(mContext,inflater.inflate(R.layout.channel_item,null));
        }else if(viewType==ACT){
            return new ActViewHolder(mContext,inflater.inflate(R.layout.act_item,null));
        } else if(viewType==SECKILL){
            return new SeckillViewHolder(mContext,inflater.inflate(R.layout.seckill_item,null));
        } else if(viewType==RECOMMEND){
            return new RecommendViewHolder(mContext,inflater.inflate(R.layout.recommend_item,null));
        } else if(viewType==HOT){
            return new HotViewHolder(mContext,inflater.inflate(R.layout.hot_item,null));
        }

        return null;
    }

    //viewholder加载数据
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position)==BANNER){
            BannerViewHolder bvh= (BannerViewHolder) holder;
            bvh.setData(resultBean.getBanner_info());
        }else if(getItemViewType(position)==CHANNEL){
            ChannelViewHolder chv= (ChannelViewHolder) holder;
            chv.setData(resultBean.getChannel_info());
        } else if(getItemViewType(position)==ACT){
            ActViewHolder avh= (ActViewHolder) holder;
            avh.setData(resultBean.getAct_info());
        } else if(getItemViewType(position)==SECKILL){
            SeckillViewHolder svh= (SeckillViewHolder) holder;
            svh.setData(resultBean.getSeckill_info());
        } else if(getItemViewType(position)==RECOMMEND){
            RecommendViewHolder rvh= (RecommendViewHolder) holder;
            rvh.setData(resultBean.getRecommend_info());
        } else if(getItemViewType(position)==HOT){
            HotViewHolder hvh= (HotViewHolder) holder;
            hvh.setData(resultBean.getHot_info());
        }
    }

    //多个条目的类型
    @Override
    public int getItemViewType(int position) {
        switch(position){
            case BANNER:
                currentType=BANNER;
                break;
            case CHANNEL:
                currentType=CHANNEL;
                break;
            case ACT:
                currentType=ACT;
                break;
            case SECKILL:
                currentType=SECKILL;
                break;
            case RECOMMEND:
                currentType=RECOMMEND;
                break;
            case HOT:
                currentType=HOT;
                break;

            default:
            
                break;
        }
        return currentType;
    }

    //条目的数量
    @Override
    public int getItemCount() {
        return 6;
    }


    /**
     *广告的viewholder
     *@author 聪明一只哈
     *@time 2018/3/31 14:26
     */
    class BannerViewHolder extends RecyclerView.ViewHolder{

        private Context mContext;
        private View itemView;
        private Banner banner;

        public BannerViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext=mContext;
            this.banner=itemView.findViewById(R.id.banner);

        }

        public void setData(List<HomeResaultBeanData.ResultBean.BannerInfoBean> banner_info) {
            //设置banner数据
            List<String> imageurl=new ArrayList<>();
            for (int i=0;i<banner_info.size();i++){
                String s = Constants.BASE_URl_IMAGE + banner_info.get(i).getImage();
                Log.i("HomeResaultB", "setData: "+s);
                imageurl.add(Constants.BASE_URl_IMAGE+banner_info.get(i).getImage());
            }
            //必须要有,不然图片出不来
            banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    Glide.with(mContext).load(path).into(imageView);
                }
            });

            //设置循环指示
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);

            //设置手风琴效果
            banner.setBannerAnimation(Transformer.Accordion);
            banner.setImages(imageurl);
            banner.start();

            //设置点击
            banner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    //poisition需要-1
                    //startGoodsActivity(hot_info);

                }
            });
        }
    }

    /**
     *跳转到商品详情页
     *@author 聪明一只哈
     *@time 2018/3/31 16:53
     * @param hot_info
     */
    private void startGoodsActivity(GoodsInfo hot_info) {
        Intent intent = new Intent(mContext, GoodsInfoActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(GOODSINFO,hot_info);
        intent.putExtras(bundle);
        mContext.startActivity(intent);
    }

    /**
     *频道的viewholder
     *@author 聪明一只哈
     *@time 2018/3/31 14:26
     */
    class ChannelViewHolder extends RecyclerView.ViewHolder{

        private Context mContext;
        private GridView gcChannel;
        private ChannelAdapter channelAdapter;

        public ChannelViewHolder(final Context mContext, View itemView) {
            super(itemView);
            this.mContext=mContext;
            gcChannel=itemView.findViewById(R.id.gv_channel);


            gcChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
                }
            });

        }

        public void setData(List<HomeResaultBeanData.ResultBean.ChannelInfoBean> channel_info) {
            channelAdapter = new ChannelAdapter(mContext,channel_info);
            gcChannel.setAdapter(channelAdapter);

        }
    }

    /**
     *活动的viewholder
     *@author 聪明一只哈
     *@time 2018/3/31 14:26
     */
    class ActViewHolder extends RecyclerView.ViewHolder{

        private Context mContext;
        private ViewPager act_viewpager;


        public ActViewHolder(final Context mContext, View itemView) {
            super(itemView);
            this.mContext=mContext;
            act_viewpager=itemView.findViewById(R.id.act_viewpager);


        }
        /**
         *加载数据
         *@author 聪明一只哈
         *@time 2018/3/31 14:50
         */
        public void setData(final List<HomeResaultBeanData.ResultBean.ActInfoBean> act_info) {

            //加特效
            //设置间距
            act_viewpager.setPageMargin(20);
            act_viewpager.setOffscreenPageLimit(3);


            //加载数据
            act_viewpager.setAdapter(new PagerAdapter() {
                @Override
                public int getCount() {
                    return act_info==null?0:act_info.size();
                }

                @Override
                public boolean isViewFromObject(View view, Object object) {
                    return view==object;
                }

                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    ImageView imageView = new ImageView(mContext);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    container.addView(imageView);
                    Glide.with(mContext).load(Constants.BASE_URl_IMAGE+act_info.get(position).getIcon_url()).into(imageView);

                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    });
                    return imageView;
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    container.removeView((View) object);
                }
            });

            act_viewpager.setPageTransformer(true,new RotateDownTransformer());

        }
    }



    /**
     *秒杀的viewholder
     *@author 聪明一只哈
     *@time 2018/3/31 14:26
     */
    class SeckillViewHolder extends RecyclerView.ViewHolder{

        private Context mContext;
        private TextView tv_time_seckill;
        private TextView tv_more_seckill;
        private RecyclerView rv_seckill;
        private SeckillVhAdapter seckillVhAdapter;

        //倒计时
        private long dt=0;

        //handler执行倒计时循环
        private Handler handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                dt=dt-1000;
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                String format = simpleDateFormat.format(new Date(dt));
                tv_time_seckill.setText(format);
                handler.removeMessages(0);
                handler.sendEmptyMessageDelayed(0,1000);
                if(dt<=0){
                    //移除消息
                    handler.removeCallbacksAndMessages(null);
                }
            }
        };


        public SeckillViewHolder(final Context mContext, View itemView) {
            super(itemView);
            this.mContext=mContext;
            tv_time_seckill=itemView.findViewById(R.id.tv_time_seckill);
            tv_more_seckill=itemView.findViewById(R.id.tv_more_seckill);
            rv_seckill=itemView.findViewById(R.id.rv_seckill);

        }

        public void setData(final HomeResaultBeanData.ResultBean.SeckillInfoBean seckill_info) {
            //得数据
            //秒倒计时 的差
            dt=Long.parseLong(seckill_info.getEnd_time())-Long.parseLong(seckill_info.getStart_time());

            handler.sendEmptyMessageDelayed(0,1000);

            //---------------------------------------
            //设置文本和adapter
            seckillVhAdapter = new SeckillVhAdapter(mContext,seckill_info.getList());
            Log.i("SeckillVhAd", "setData: "+seckill_info.getList().get(0).getFigure());
            rv_seckill.setAdapter(seckillVhAdapter);
            //设置布局管理器
            rv_seckill.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));//false 这个值表示是否是倒叙

            //接口回调
            seckillVhAdapter.setOnseckilllistener(new SeckillVhAdapter.OnSeckillListener() {
                @Override
                public void onItemClick(int position) {
                    Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
                    GoodsInfo goodsInfo = new GoodsInfo();
                    HomeResaultBeanData.ResultBean.SeckillInfoBean.ListBean listBean = seckill_info.getList().get(position);
                    if(listBean!=null){
                        goodsInfo.setName(listBean.getName());
                        goodsInfo.setFigure(listBean.getFigure());
                        goodsInfo.setProduct_id(listBean.getProduct_id());
                        goodsInfo.setCover_price(listBean.getCover_price());

                    }
                    startGoodsActivity(goodsInfo);
                }
            });
        }

    }

    /**
     *推荐的viewholder
     *@author 聪明一只哈
     *@time 2018/3/31 14:26
     */
    class RecommendViewHolder extends RecyclerView.ViewHolder{

        private Context mContext;
        private GridView gv_recommend;
        private TextView tv_more_recommend;
        private RecomVhAdapter vhAdapter;

        public RecommendViewHolder(final Context mContext, View itemView) {
            super(itemView);
            this.mContext=mContext;
            tv_more_recommend=itemView.findViewById(R.id.tv_more_recommend);
            gv_recommend=itemView.findViewById(R.id.gv_recommend);




        }

        public void setData(final List<HomeResaultBeanData.ResultBean.RecommendInfoBean> recommend_info) {
            //获得数据

            //添加数据到列表
            vhAdapter = new RecomVhAdapter(mContext, recommend_info);
            gv_recommend.setAdapter(vhAdapter);

            gv_recommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
                    GoodsInfo goodsInfo = new GoodsInfo();
                    HomeResaultBeanData.ResultBean.RecommendInfoBean recommendInfoBean = recommend_info.get(position);
                    if(recommendInfoBean!=null){
                        goodsInfo.setName(recommendInfoBean.getName());
                        goodsInfo.setFigure(recommendInfoBean.getFigure());
                        goodsInfo.setProduct_id(recommendInfoBean.getProduct_id());
                        goodsInfo.setCover_price(recommendInfoBean.getCover_price());

                    }
                    startGoodsActivity(goodsInfo);
                }
            });
        }
    }

    /**
     *热卖的viewholder
     *@author 聪明一只哈
     *@time 2018/3/31 14:26
     */
    class HotViewHolder extends RecyclerView.ViewHolder{

        private Context mContext;
        private TextView tv_more_hot;
        private GridView gv_hot;
        private HotVhAdapter hotVhAdapter;


        public HotViewHolder(final Context mContext, View itemView) {
            super(itemView);
            this.mContext=mContext;
            tv_more_hot=itemView.findViewById(R.id.tv_more_hot);
            gv_hot=itemView.findViewById(R.id.gv_hot);



        }


        public void setData(final List<HomeResaultBeanData.ResultBean.HotInfoBean> hot_info) {
            //获得数据

            //加载列表
            hotVhAdapter = new HotVhAdapter(mContext,hot_info);
            gv_hot.setAdapter(hotVhAdapter);

            gv_hot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(mContext, ""+position, Toast.LENGTH_SHORT).show();
                    GoodsInfo goodsInfo = new GoodsInfo();
                    HomeResaultBeanData.ResultBean.HotInfoBean hotInfoBean = hot_info.get(position);
                    if(hot_info!=null){
                        goodsInfo.setName(hotInfoBean.getName());
                        goodsInfo.setFigure(hotInfoBean.getFigure());
                        goodsInfo.setProduct_id(hotInfoBean.getProduct_id());
                        goodsInfo.setCover_price(hotInfoBean.getCover_price());

                    }
                    startGoodsActivity(goodsInfo);
                }
            });
        }
    }
}
